package com.caiying.dota.spider.parser.youku;

import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.caiying.dota.net.HttpConnector;
import com.caiying.dota.spider.entity.Video;

public class YoukuParser {
	
	/**0 一个优酷用户id ---> 该用户所有的视频 *///1+2
	public static ArrayList<Video> getYoukuVideos(String youku_id){
		ArrayList<Video> videos = new ArrayList<Video>();
		ArrayList<String> urls = getYoukuUrlsByUserid(youku_id);
		for(String url : urls){
			videos.addAll(parserYoukuVideo(HttpConnector.getString(url), youku_id));
		}
		return videos;
	}
	
	/**1 一个优酷用户id ---> 优酷视频页面 */
	public static ArrayList<String> getYoukuUrlsByUserid(String userid){
		ArrayList<String> urls = new ArrayList<String>();
		String url = "http://i.youku.com/u/" + userid + "/videos";
//		Logger.i("getYoukuUrlsByUserid()", url);
		urls.add(url);
		Document doc = Jsoup.parse(HttpConnector.getString(url));
		Element body = doc.body(); 
		Element pages = body.getElementsByClass("pages").first();
		Elements lis = pages.getElementsByTag("li");
		for(Element li : lis){
			if(li.getElementsByTag("a").size() != 0){
//				Logger.i("getYoukuUrlsByUserid()", Youku.base_url + li.getElementsByTag("a").first().attr("href"));
				urls.add(Youku.base_url + li.getElementsByTag("a").first().attr("href"));
			}
		}
		return urls;
	}
	
	/**2 一个优酷html页面 ---> 其中的视频地址 */
	public static ArrayList<Video> parserYoukuVideo(String content, String youku_id){
		ArrayList<Video> videos = new ArrayList<Video>();
		Document doc = Jsoup.parse(content);  
		Element body = doc.body();  
	    Elements elements = body.getElementsByClass("v");
	    int count = 0;
//	    VideosDAO videosDAO = new VideosDAO(AppContext.getContext());
//	    VideoUrlsDAO videoUrlsDAO = new VideoUrlsDAO(AppContext.getContext());
		for(Element element : elements){
			String link = element.getElementsByClass("v_link").first().getElementsByTag("a").first().attr("href");
			String thumb = element.getElementsByClass("v_thumb").first().getElementsByTag("img").first().attr("src");
			String ishd = "";
			if(element.getElementsByClass("v_ishd").first().getElementsByTag("span").size() != 0){
				element.getElementsByClass("v_ishd").first().getElementsByTag("span").first().attr("title");
			}
			String menu = element.getElementsByClass("v_menu").first().attr("id");
			String time = element.getElementsByClass("v_time").first().getElementsByTag("span").first().text();
			String title = element.getElementsByClass("v_title").first().getElementsByTag("a").first().attr("title");
			String pub = element.getElementsByClass("v_pub").first().getElementsByTag("span").first().text();
			String num_play = element.getElementsByClass("v_stat").first().getElementsByTag("span").get(1).text();
			String num_comment = element.getElementsByClass("v_stat").first().getElementsByTag("span").get(3).text();
			Video video = new Video(youku_id, thumb, link, "intro", time, pub, menu, num_play, num_comment, ishd, title, "segs", "logo");
	    	videos.add(video);
//	    	videosDAO.insert(video);
//	    	videoUrlsDAO.insert(new VideoUrls(youku_id, video.link));
	    	
//	    	Logger.i(Logger.TAG_TIPS, (count++) +" =====================");
//			Logger.i("jsoup_link", link);
//			Logger.i("jsoup_thumb", thumb);
//			Logger.i("jsoup_ishd", ishd);
//			Logger.i("jsoup_menu", menu);
//			Logger.i("jsoup_time", time);
//			Logger.i("jsoup_title", title);
//			Logger.i("jsoup_pub", pub);
//			Logger.i("jsoup_num_play", num_play);
//			Logger.i("jsoup_num_comment", num_comment);
	    }
		return videos;
	}
}
